﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using FrontRoomCheck.Utiltiy;
using YJKCodeBase.WinForm;
using YJKDrawSelectArea;
using YJKRevitCode.FormUtility.ModelessFormControlSuite.ModelLayer;

namespace FrontRoomCheck.ShowDataForm.ModellessFrame
{
	internal class ManualAddFrontRoomStackFactory : IOperateStackFactory
	{
		internal ManualAddFrontRoomStackFactory(ExternalCommandData inputCommand, ManualAddFrontRoomForm_ModellessVersion inputForm)
		{
			this.m_Command = inputCommand;
			this.m_useForm = inputForm;
		}

		public PickObjectMethod GetAPickMethod(int caseIndex)
		{
			if (caseIndex == 0)
			{
				return new PickObjectMethod(this.MarqueeRoomMethod);
			}
			return new PickObjectMethod(this.DrawLineMethod);
		}

		public IOperateStack GetAStack(int caseIndex)
		{
			if (caseIndex == 0)
			{
				return new MarqueeRoomStack(this.m_useForm);
			}
			return new DrawLineStack(this.m_useForm);
		}

		private List<Element> MarqueeRoomMethod()
		{
			SelectionFilterRoom selectionFilterRoom = new SelectionFilterRoom();
			if (this.m_justChangedTag)
			{
				this.m_justChangedTag = false;
				try
				{
					this.m_useForm.UiDoc.Selection.PickElementsByRectangle(selectionFilterRoom, "请框选房间");
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
				}
			}
			IList<Element> source = this.m_useForm.UiDoc.Selection.PickElementsByRectangle(selectionFilterRoom, "请框选房间");
			return source.ToList<Element>();
		}

		private List<Curve> DrawLineMethod()
		{
			Document document = this.m_useForm.UiDoc.Document;
			SelectAreaManager selectAreaManager = new SelectAreaManager(this.m_Command);
			SelectAreaManager._hWndRevit = (this.m_useForm.RevitHandle as WindowHandle);
			List<Curve> list = new List<Curve>();
			selectAreaManager.EditOutLine();
			List<ElementId> lstDetailCurveId = selectAreaManager.m_lstDetailCurveId;
			foreach (ElementId elementId in lstDetailCurveId)
			{
				DetailCurve detailCurve = document.GetElement(elementId) as DetailCurve;
				if (detailCurve != null)
				{
					list.Add(detailCurve.GeometryCurve);
				}
			}
			Transaction transaction = new Transaction(document, "tempDelete");
			try
			{
				transaction.Start();
				document.Delete(lstDetailCurveId);
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
			if (this.m_useForm.IfContinue)
			{
				this.m_justChangedTag = true;
				throw new EscException();
			}
			return list;
		}

		private ExternalCommandData m_Command;

		private ManualAddFrontRoomForm_ModellessVersion m_useForm;

		private bool m_justChangedTag;
	}
}
